/**
 * User: alesj
 * Date: Sep 23, 2003
 * Time: 10:39:06 AM
 */

package com.generalynx.common;

import java.util.Calendar;
import java.util.Locale;
import java.util.Properties;
import java.util.SimpleTimeZone;
import java.util.StringTokenizer;
import java.util.TimeZone;

import com.generalynx.common.utils.Const;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CommonSettings {
    public static final Log logger = LogFactory.getLog(CommonSettings.class);

    private static String KEY_PREFIX = "common.";
    //read from settings.ini
    public static Locale m_Locale;
    public static TimeZone m_timeZone;

    // action servlet
    public static String m_sActionServlet;

    // db
    public static String m_sDBDriver;
    public static String m_sDBurlFormat;
    public static String m_sDB_Server;
    public static String m_sDB_Database;
    public static String m_sDBUsername;
    public static String m_sDBPassword;

    //sending sms properties
    public static String m_sSMSusername;
    public static String m_sSMSpassword;
    public static int m_sSMStype;

    //sending email
    public static String m_sMailServer;

    public static String m_sBillingDBUrl;
    public static String m_sSiMobilSMSXml;

    //icons
    public static String m_sGraphIcon;

    //alarm
    public static boolean m_bAlarmsOn;

    //optional logger for stack trace logging
    public static String m_stackTraceLog;

    public static String m_defaultlDBUrl;

    private CommonSettings() {
    }

    /**
     * reading settings from properties object
     *
     * @param properties
     */
    public static void init(Properties properties) {
        logger.info("Reading common setting ...");

        String sLocale = properties.getProperty(KEY_PREFIX + "locale");

        m_sActionServlet = properties.getProperty(KEY_PREFIX + "action_servlet");

        m_sDBDriver = properties.getProperty(KEY_PREFIX + "db_driver");
        m_sDBurlFormat = properties.getProperty(KEY_PREFIX + "db_url_format");
        m_sDB_Server = properties.getProperty(KEY_PREFIX + "db_server");
        m_sDB_Database = properties.getProperty(KEY_PREFIX + "db_database");
        m_sDBUsername = properties.getProperty(KEY_PREFIX + "db_username");
        m_sDBPassword = properties.getProperty(KEY_PREFIX + "db_password");

        //reading sms properties
        m_sSMSusername = properties.getProperty(KEY_PREFIX + "sms_username");
        m_sSMSpassword = properties.getProperty(KEY_PREFIX + "sms_password");
        m_sSMStype = Integer.parseInt(properties.getProperty(KEY_PREFIX + "sms_type"));

        m_bAlarmsOn = Boolean.valueOf(properties.getProperty(KEY_PREFIX + "alarm_on")).booleanValue();
        logger.info("Alarm switch on: " + m_bAlarmsOn);

        m_sMailServer = properties.getProperty(KEY_PREFIX + "mail_smtp_server");

        m_sBillingDBUrl = CommonTools.createFullDBUrl(m_sDB_Server, properties.getProperty(KEY_PREFIX + "billing_db"), m_sDBUsername, m_sDBPassword);
        //TODO not yet implemented
        m_sSiMobilSMSXml = properties.getProperty(KEY_PREFIX + "simobil_xml");

        //icons
        m_sGraphIcon = properties.getProperty(KEY_PREFIX + "graph_icon");

        m_stackTraceLog = properties.getProperty(KEY_PREFIX + "stack_trace_log");

        m_defaultlDBUrl = CommonTools.createFullDBUrl(m_sDB_Server, m_sDB_Database, m_sDBUsername, m_sDBPassword);

        Locale.setDefault(m_Locale = getLocale(sLocale));
        logger.info("Locale: " + m_Locale);
        TimeZone.setDefault(m_timeZone = getTimeZone(m_Locale));
        logger.info("TimeZone: " + m_timeZone);
    }

    private static Locale getLocale(String sLocale) {
        Locale locale;

        if (sLocale == null) {
            locale = Locale.getDefault();
        } else {
            StringTokenizer tokenizer = new StringTokenizer(sLocale, "_");

            String sLanguage = tokenizer.nextToken();
            String sCountry = tokenizer.nextToken();

            if (tokenizer.hasMoreTokens()) {
                String sVariant = tokenizer.nextToken();
                locale = new Locale(sLanguage, sCountry, sVariant);
            } else {
                locale = new Locale(sLanguage, sCountry);
            }
        }

        return locale;
    }

    private static TimeZone getTimeZone(Locale locale) {
        TimeZone timeZone;

        if (locale.getCountry().equals("SI")) {
            timeZone = new SimpleTimeZone(1 * Const.MILLIS_PER_HOUR,
                    "Europe/Ljubljana",
                    Calendar.MARCH, -1, Calendar.SUNDAY, 2 * Const.MILLIS_PER_HOUR,
                    Calendar.OCTOBER, -1, Calendar.SUNDAY, 3 * Const.MILLIS_PER_HOUR);
        } else {
            timeZone = TimeZone.getDefault();
        }

        return timeZone;
    }

}
